#!/bin/sh
#
#    Copyright (C) 2009  Vitaly Druzhinin
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#    On Debian systems, the complete text of the GNU General Public
#    License can be found in /usr/share/doc/flex-fw/GPL-3 file.
#
#    On Slackware systems, the complete text of the GNU General Public
#    License can be found in /usr/doc/flex-fw/GPL-3 file.
#
USERID=`id -u`
if test ! "$USERID" = "0"
then
    echo "Unsufficient privileges: you must became a root..."
    exit 1
fi


echo "Try to make the CentOS package"

SRCDIR="./debian"
PKGNUM=1dve
VER=""
CURDIR=`pwd`

echo -n "Step 1: Check if rpmbuild is installed: "
if test `which rpmbuild|wc -l` -ne "1"
then
    echo "Fail! Rpmbuild utility is not installed!"
    exit 1
fi
echo "Done."

echo -n "Step 2: Get package version: "
if test -z "$VER"
then
    VER=`grep "^Version: \([0-9]\+\.\)\{2\}[0-9]\+" ./$SRCDIR/DEBIAN/control | cut -d " " -f 2`
    if test -z "$VER"
    then
        echo "Fail! Invalid package version from ./debian/DEBIAN/control!"
        exit 1
    fi
fi
echo "Done. Version $VER."


# Due to RPM package naming convention
# http://www.rpm.org/max-rpm/ch-rpm-file-format.html
# name-version-release.architecture.rpm 
PKGNAME=flex-fw-$VER-$PKGNUM.noarch.rpm


echo -n "Step 3. Make temporary directory: "
TMPROOT=`mktemp -d`
if test $? -gt 0
then
    echo "Fail! Error creating temporary directory!"
    exit 1
fi
mkdir $TMPROOT/centos
TMPDIR=$TMPROOT/centos
echo "Done. $TMPDIR created"


echo -n "Step 4. Copy source tree $SRCDIR to $TMPDIR: "
cp -ax $SRCDIR/* $TMPDIR
if test $? -gt 0
then
    echo "Fail! Check access rights or disk free space!"
    exit 1
fi
echo "Done."


echo -n "Step 5. Remove .svn: "
find $TMPDIR -name '.*' -exec rm -rf {} \; >/dev/null 2>&1
echo "Done."


echo -n "Step 6. Remove backups: "
find $TMPDIR -name '*~' -exec rm -rf {} \; >/dev/null 2>&1
echo "Done."


echo -n "Step 7. Set current version to $VER: "
sed -i "s/X\.X\.X/$VER/" $TMPDIR/etc/flex-fw/vars/base/version
echo "Done."


echo -n "Step 8. Remove Debian specific directories and files: "
rm -rf $TMPDIR/DEBIAN
echo "Done."


echo -n "Step 9. Create .spec file: "
SPECFILE=$TMPDIR/flex-fw-$VER-$PKGNUM.spec
# depends from Debian package
REQUIRES=`grep '^Depends:' $SRCDIR/DEBIAN/control | sed 's/Depends://'`

cat <<END_OF_TEXT >$SPECFILE
Buildroot: $TMPDIR
Name: flex-fw
Version: $VER 
Release: $PKGNUM 
Summary: flex-fw is a front-end application for iptables utility
License: see /usr/share/doc/flex-fw/COPYRIGHT
BuildArch: noarch
Provides: flex-fw = $VER-$PKGNUM
AutoReqProv: no
Requires: $REQUIRES 
Requires(interp): /bin/sh /bin/sh
Requires(post): /bin/sh
Requires(preun): /bin/sh

%define _rpmdir ../
%define _rpmfilename flex-fw-$VER-$PKGNUM.noarch.rpm
%define _unpackaged_files_terminate_build 0

%post
END_OF_TEXT

cat $SRCDIR/DEBIAN/postinst >> $SPECFILE 

sed -i "/^if test \"\$1\" = \"configure\"/,/^fi$/ c configure" $SPECFILE

echo >> $SPECFILE

cat <<END_OF_TEXT >>$SPECFILE
echo -n "Add flex-fw in services: "
chkconfig --add flex-fw
echo "Ok"
echo


END_OF_TEXT

echo '%preun' >> $SPECFILE
cp $SRCDIR/DEBIAN/prerm $TMPROOT/prerm 

sed -i "/^if test \"\$1\" = \"upgrade\"/,/then/d" $TMPROOT/prerm
sed -i '/^fi/d' $TMPROOT/prerm
cat $TMPROOT/prerm >> $SPECFILE

cat <<END_OF_TEXT >>$SPECFILE
echo -n "Delete flex-fw from services: "
chkconfig --del flex-fw
echo "Ok"
echo 

%description


%files
END_OF_TEXT

# all directories from source, exclude:
# /etc/init.d
cd $TMPDIR
for D in `find . -type d|sed 's/^\.//;/^\/etc\/init\.d$/d'`
    do
        echo "%dir \"$D\"" >> $SPECFILE
    done
# all files from source exclude spec file
for F in `find . ! -name '*.spec' -type f -or -type l|sed 's/^\.//'` 
    do
        echo "\"$F\"" >> $SPECFILE
    done

cd $CURDIR
echo "Done."


echo -n "Step 10. Set permissions to root:root: "
chown -R root:root $TMPDIR/*
echo "Done."

echo "Step 11. Make package $PKGNAME: "
cd $TMPDIR
rpmbuild -v --buildroot $TMPDIR -bb $SPECFILE
echo "Done."
cd $CURDIR

echo -n "Step 12. Move package in work directory, remove $TMPDIR: "
mv $TMPROOT/*.rpm $CURDIR
rm -rf $TMPROOT
echo "Done."
